package me.lc.linklink.layer;

import java.util.ArrayList;
import java.util.List;

import me.lc.linklink.R;
import me.lc.linklink.core.GameSystem;
import me.lc.linklink.sprite.StageButtonNode;

import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.NinePatchSprite;
import com.wiyun.engine.nodes.ScrollableLayer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYColor4B;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.utils.ResolutionIndependent;

/**
 * 关卡选择层
 * 
 * @author Administrator
 * 
 */
public class StageMenuLayer extends Layer {

	List<StageButtonNode> btnNodes = new ArrayList<StageButtonNode>();

	// public StageMenuLayer() {
	// Sprite bg = Sprite.make(GameSystem.stageMenuBgTex);
	// bg.setAnchorPercent(0, 0);
	// float bgX = (GameSystem.Screen_Width - GameSystem.stageMenuBgTex
	// .getWidth()) / 2.0f;
	// float bgY = GameSystem.Screen_ActuallyHeight * 0.135f;
	// bg.setPosition(bgX, bgY);
	// addChild(bg);
	//
	// float marginTopButtom = GameSystem.Screen_ActuallyHeight * 0.0375f;
	// float marginLeftRight = GameSystem.Screen_Width * 0.0375f;
	// float marginBtnHorizon = ((GameSystem.stageMenuBgTex.getWidth() -
	// marginLeftRight * 2.0f) - (GameSystem.stageButtonTex
	// .getWidth() * 4.0f)) / 3f;
	// float marginBtnVertical = ((GameSystem.stageMenuBgTex.getHeight() -
	// marginTopButtom * 2.0f) - (GameSystem.stageButtonTex
	// .getHeight() * 5.0f)) / 4f;
	// float buttonStartX = bgX + marginLeftRight;
	// float buttonStartY = bgY + marginTopButtom
	// + GameSystem.stageButtonTex.getHeight() * 4.0f
	// + marginBtnVertical * 4.0f;
	//
	// int columns = 4;
	// int stageCount = 20;
	// for (int i = 0; i < stageCount; i++) {
	// StageButtonNode buttonNode = new StageButtonNode(i + 1);
	// int column = i % columns;
	// int row = i / columns;
	// buttonNode.setAnchorPercent(0, 0);
	// float x = buttonStartX
	// + (GameSystem.stageButtonTex.getWidth() + marginBtnHorizon)
	// * column + (GameSystem.stageButtonTex.getWidth() / 2.0f);
	// float y = buttonStartY
	// - (GameSystem.stageButtonTex.getHeight() + marginBtnVertical)
	// * row + (GameSystem.stageButtonTex.getHeight() / 2.0f);
	// buttonNode.setPosition(x, y);
	// addChild(buttonNode);
	// btnNodes.add(buttonNode);
	// }
	// }

	ScrollableLayer sLayer;
	Label helpLabel;

	public StageMenuLayer() {
		Sprite bg = Sprite.make(GameSystem.stageMenuBgTex);
		bg.setAnchorPercent(0, 0);
		float bgX = (GameSystem.Screen_Width - GameSystem.stageMenuBgTex
				.getWidth()) / 2.0f;
		float bgY = GameSystem.Screen_ActuallyHeight * 0.135f;
		bg.setPosition(bgX, bgY);
		addChild(bg);

		sLayer = ScrollableLayer.make(WYColor4B.make(0, 0, 0, 0));
		sLayer.setAnchorPercent(0, 0);
		sLayer.setContentSize(GameSystem.stageMenuBgTex.getWidth(),
				GameSystem.stageMenuBgTex.getHeight());
		// sLayer.setRelativeAnchorPoint(true);
		sLayer.setPosition(bgX, bgY);
		sLayer.setVertical(true);
		// layer.setCallback(this);
		addChild(sLayer);

		NinePatchSprite thumb = NinePatchSprite.make(
				Texture2D.makePNG(R.drawable.vertical_thumb),
				WYRect.make(DP(5), DP(7), DP(1), DP(1)));
		sLayer.setVerticalThumb(thumb);

		float marginTopButtom = GameSystem.Screen_ActuallyHeight * 0.0375f;
		float marginLeftRight = GameSystem.Screen_Width * 0.0375f;
		float marginBtnHorizon = ((GameSystem.stageMenuBgTex.getWidth() - marginLeftRight * 2.0f) - (GameSystem.stageButtonTex
				.getWidth() * 4.0f)) / 3f;
		float marginBtnVertical = ((GameSystem.stageMenuBgTex.getHeight() - marginTopButtom * 2.0f) - (GameSystem.stageButtonTex
				.getHeight() * 5.0f)) / 4f;
		// float buttonStartX = bgX + marginLeftRight;
		float buttonStartX = marginLeftRight;
		// float buttonStartY = bgY + marginTopButtom
		// + GameSystem.tex_stageButton.getHeight() * 4.0f
		// + marginBtnVertical * 4.0f;

		int columns = 4;
		int stageCount = 100;

		sLayer.setTopMargin(marginTopButtom * 2);
		sLayer.setBottomMargin(marginTopButtom * 2);

		for (int i = 0; i < stageCount; i++) {
			StageButtonNode buttonNode = new StageButtonNode(i + 1);
			int column = i % columns;
			int row = i / columns;

			int rows = (stageCount / columns)
					+ ((stageCount % columns == 0) ? 0 : 1);

			buttonNode.setAnchorPercent(0, 0);
			float x = buttonStartX
					+ (GameSystem.stageButtonTex.getWidth() + marginBtnHorizon)
					* column + (GameSystem.stageButtonTex.getWidth() / 2.0f);
			// float y = buttonStartY
			// - (GameSystem.tex_stageButton.getHeight() + marginBtnVertical)
			// * row + (GameSystem.tex_stageButton.getHeight() / 2.0f);
			float y = (rows - 1 - row)
					* (GameSystem.stageButtonTex.getHeight() + marginBtnVertical);
			buttonNode.setPosition(x, y);
			sLayer.addScrollableChild(buttonNode);
			btnNodes.add(buttonNode);
		}

		// 提示信息
		helpLabel = Label.make("滑动界面显示更多关卡", 28);
		helpLabel.setColor(WYColor3B.make(200, 10, 10));
		helpLabel.setPosition(GameSystem.Screen_Width / 2.0f, bgY
				+ GameSystem.stageMenuBgTex.getHeight() + 22.5f);
		addChild(helpLabel);
	}

	private float DP(float v) {
		return ResolutionIndependent.resolveDp(v);
	}

	public void refreshBtns() {
		for (StageButtonNode node : btnNodes) {
			node.refresh();
		}
	}
}
